#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<cstring>
#include<arpa/inet.h>// inet_pton函数
#include<netinet/in.h>  //sockaddr_in 结构体
using namespace std;
#define SERVER_PORT 8080
int main()
{
    //创建套接字
    int sockfd=socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd==-1)
    {
        perror("socked error");
        _exit(-1);
    }
    //连接服务器
    struct sockaddr_in serverAddress;
    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(SERVER_PORT);
    const char *ip = "192.168.232.129";
    //将ip填入serverAddress.sin_addr.s_addr
    inet_pton(AF_INET, ip, &(serverAddress.sin_addr.s_addr));
    int ret=connect(sockfd, reinterpret_cast<const sockaddr *>(&serverAddress), sizeof(serverAddress));
    if(ret!=0)
    {
        perror("connect error:");
        _exit(-1);
    }
    char writebuffer[128] = {0};
    char readbuffer[128] = {0};
    while (1)
    {
        cout<< "请输入要发送的信息：";
        cin >> writebuffer;
        write(sockfd, writebuffer,strlen(writebuffer) + 1);
        read(sockfd, readbuffer, sizeof(readbuffer));
        cout << "server:" << readbuffer << endl;
        usleep(5000);
    }
    return 0;
}